package kg.aibek.poker;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Hand {
	private List<Card> cards = new ArrayList<Card>(5);
	private int level;
	private boolean pair, twoPair, threeOfKind, straight, flush, fullHouse,
			fourOfKind, straighFlush, royalFlush;
	private long value;

	public Hand(String str) {
		String[] ss = str.trim().split(" ");

		for (String sss : ss) {
			cards.add(new Card(sss));
		}

		Map<Integer, Integer> map = new TreeMap<Integer, Integer>();

		flush = true;
		straight = true;
		Collections.sort(cards);

		String prevSuit = cards.get(0).getSuit();
		int prevValue = cards.get(0).getValue() - 1;
		for (int i = 0; i < cards.size(); i++) {
			int value = cards.get(i).getValue();

			flush = flush && cards.get(i).getSuit().equals(prevSuit);
			straight = straight && (value - prevValue == 1);
			prevValue = value;

			if (map.containsKey(value)) {
				map.put(value, map.get(value) + 1);
			} else {
				map.put(value, 1);
			}
		}

		straighFlush = flush && straight;
		royalFlush = straighFlush && cards.get(4).getSuit().equals("A");

		value = 0;
		if (map.size() == 2) {
			for (Integer key : map.keySet()) {
				if (map.get(key) == 1)
					fourOfKind = true;
				if (map.get(key) == 2)
					fullHouse = true;

				if (map.get(key) < 3) {
					value += key;
				} else {
					value += 100 * key;
				}
			}
		}

		if (map.size() == 3) {
			for (Integer key : map.keySet()) {
				if (map.get(key) == 3)
					threeOfKind = true;

				if (map.get(key) == 2) {
					twoPair = true;
				}
			}
			int ind = 0;

			for (Integer key : map.keySet()) {
				if (threeOfKind) {
					if (map.get(key) == 1) {
						value += Math.pow(10, ind) * key;
						ind += 2;
					} else {
						value += Math.pow(10, 4) * key;
					}
				} else {
					if (map.get(key) == 1) {
						value += Math.pow(10, 0) * key;
					} else {
						ind += 2;
						value += Math.pow(10, ind) * key;
					}
				}
			}
		}

		if (map.size() == 4) {
			pair = true;
			int ind = 0;
			for (Integer key : map.keySet()) {
				if (map.get(key) == 2) {
					value += Math.pow(10, 6) * key;
				} else {
					value += Math.pow(10, ind) * key;
					ind += 2;
				}
			}
		}

		if (map.size() == 5) {
			int ind = 0;
			for (Integer key : map.keySet()) {
				value += Math.pow(10, ind) * key;
				ind += 2;
			}
		}

		if (royalFlush) {
			level = 9;
		} else if (straighFlush) {
			level = 8;
		} else if (fourOfKind) {
			level = 7;
		} else if (fullHouse) {
			level = 6;
		} else if (flush) {
			level = 5;
		} else if (straight) {
			level = 4;
		} else if (threeOfKind) {
			level = 3;
		} else if (twoPair) {
			level = 2;
		} else if (pair) {
			level = 1;
		}
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public long getValue() {
		return value;
	}

	public void setValue(long value) {
		this.value = value;
	}

	public int compareTo(Hand o) {
		if (level > o.getLevel())
			return 1;
		if (level < o.getLevel())
			return -1;
		if (value > o.getValue())
			return 1;
		if (value < o.getValue())
			return -1;
		return 0;
	}

}
